﻿using System;

namespace Calendar
{
    /// <summary>
    /// 输出日历
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            int year = DateTime.Today.Year;        //获取所在年份
            int month = DateTime.Today.Month;      //获取所在月份
            //输出表头
            Console.Write(year + "年");
            Console.WriteLine(month + "月");
            Console.WriteLine(" 一 二 三 四 五 六 日");
            //当月一号所在列
            int firstDay = Convert.ToInt32(new DateTime(year, month, 1).DayOfWeek - 1);
            //当月总天数
            int totalDays = DateTime.DaysInMonth(year, month);
            //上月总天数
            int lastMonthtotalDays = Convert.ToInt32(DateTime.DaysInMonth(year - (12 - month) / 11, month - 1 + (1 - (month + 10) / 12) * 12));
            int day = 1;                           //声明本月日期数变量
            int dayofNextMonth = 1;                //声明下月日期变量
            
            for (int i = 0; i < 6; i++)            //行的循环
            {
                for (int j = 0; j < 7; j++)        //列的循环
                {
                    if (i == 0 && j < firstDay)    //输出上个月的月末
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.Write(Convert.ToString(lastMonthtotalDays - firstDay + 1 + j).PadLeft(3));
                    }
                    else if (day == DateTime.Today.Day)//输出当日
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.BackgroundColor = ConsoleColor.Blue;
                        Console.Write(Convert.ToString(day).PadLeft(3));
                        day++;
                    }
                    else if (day <= totalDays && (i > 0 || j >= firstDay))  //输出当月其他日期
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.BackgroundColor = ConsoleColor.Black;
                        Console.Write(Convert.ToString(day).PadLeft(3));
                        day++;
                    }
                    else                            //输出下月
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.BackgroundColor = ConsoleColor.Black;
                        Console.Write(Convert.ToString(dayofNextMonth).PadLeft(3));
                        dayofNextMonth++;
                    }
                }
                Console.WriteLine();
            }
        }
    }
}
